<?php
$this->headTitle('Sửa thiết bị','append');
?>
<!-- Start Forms -->
<div class="grid grid_7">
    <div class="box-header">Cập nhật thiết bị</div>
    <div class="box">
        <?php
            if(isset($this->errorDate)){
                echo '<div class="notification error"><span class="strong">'.$this->errorDate.'</span></div>';
            }
            if($this->message !=''){
            	echo '<div class="notification error"><span class="strong">'.$this->message.'</span></div>';
            }
            echo $this->form;

        ?>
		<script language="javascript">
        
           
            $("#isGeneral").change(function (){
                $("#isGeneral option:selected").each(function () {
                    value = $(this).attr("value");
                    if(value == "1"){
                        $("#params").removeAttr("disabled");
                    }else{
                        $("#params option").removeAttr("selected");
                        $("#params").attr("disabled","disabled");
                    }
                });
                return false;
            });
			$("form > dl.zend_form > dd#ipWan-element").append('</br><input type="button" id="insertItem" name="insertItem" value ="Thêm thành phần thiết bị (Nếu có)" />');
            
			$("#trackDate").datepicker({ changeMonth : true, changeYear: true, yearRange: '2013:3000' });
            $("form > dl.zend_form > dd#trackDate-element").append('&nbsp;&nbsp;<input type="button" id="delNotify" name="delNotify" value ="Bỏ theo dõi"/>');
            $("form > dl.zend_form > dd#trackDate-element #delNotify").click(function(){
                $(this).parent().find("#trackDate").val('');
                $('#trackContent').text("");
            });
			$('#insertItem').click(function(){
				$("form > dl.zend_form > dd#ipWan-element").append('<div class="fwrap"><div class = "mTit">Thông tin thành phần thiết bị</div><div class="content">'
														+'<div class="row-2" style="margin-bottom: 10px;"><div class="row-first"><label class="first-row-label">Tên</label> <input type="text" name="item[]" size="20" /></div>'
                                                        +'<div class="row-second"><label>Serial number</label> <input type="text" name="serialNumber[]" size="20" /></div><div style="clear:both;"></div></div>'
														+'<div style="clear:both;"></div><div class = "row-2"><div class="row-first"><label class="first-row-label">Giá Mua</label>  <input type="text" name="priceItem_new[]" size="20" /><div class="rcost"></div></div>'
														+'<div class="row-second"><label>Giá Thuê</label> <input type="text" name="rentItem_new[]" size="20" /><div class="rcost"></div></div>'
														+'<div style="clear:both;"></div><div class="row-first"><label class="first-row-label">Giá Hiện Tại</label> <input type="text" name="priceNow_new[]" size="20" /><div class="rcost"></div></div>'
														+'<div class="row-second"><label>Giá sau hợp đồng</label> <input type="text" name="rentEnd_new[]" size="20" /><div class="rcost"></div></div>'
                                                        +'<div class="clr"></div></div><div class = "row-3"><div class="row-first"><label class="first-row-label">Ngày Bắt Đầu</label> <input type="text" name="startDate_new[]" size="20" class="startDate" readOnly ="true" /></div>'
														+'<div class="row-second"><label>Ngày kết thúc</label> <input type="text" name="duration_new[]" size="20" class="duration" /></div>'
														+'<div class="clr"></div></div><div class = "row-5"><div class="row-first"><label class="first-row-label">Tài Sản</label> <input type="text" name="owner_new[]" size="20" /></div>'
														+'<div class="row-second"><label>Người ký hợp đồng</label> <input type="text" name="holders_new[]" size="20" /></div>'
														+'<div class="clr"></div></div><div class = "row-4"><label class="first-row-label">Lịch sử</label> <textarea name="history_new[]" rows="3" cols ="50"></textarea></br>'
														+'<label class="first-row-label">Thông Tin</label> <textarea name="info_new[]" rows="3" cols ="50"></textarea></br>'
														+'<label class="first-row-label">Phụ lục</label> <textarea name="addendums_new[]" rows="3" cols ="50"></textarea>'
														+'<div style="clear: both;"></div><label class="red first-row-label">Thuê chỗ</label> <input type="checkbox" name="endTime_new[]" />'
														+'</div><input class="del"  type="button" onclick="del(this);" value ="Xóa" /></div>');
               
				
                $(function () {
		            $(".startDate").datepicker({ changeMonth : true, changeYear: true, yearRange: '1950:3000' });
					$(".duration").datepicker({ changeMonth : true, changeYear: true, yearRange: '1950:3000' });
                    $('input[name^="priceItem"], input[name^="rentItem"], input[name^="priceNow"], input[name^="rentEnd"]').keydown(function(event) {
                    	if( !(event.keyCode == 8                                // backspace
                            || event.keyCode == 9                               // tab
                            || event.keyCode == 46                              // delete
                            || (event.keyCode >= 35 && event.keyCode <= 40)     // arrow keys/home/end
                            || (event.keyCode >= 48 && event.keyCode <= 57)     // numbers on keyboard
                            || (event.keyCode >= 96 && event.keyCode <= 105))   // number on keypad
                            ) {
                                event.preventDefault();     // Prevent character input
                        }
                    });
                    
                    $('input[name^="priceItem"], input[name^="rentItem"], input[name^="priceNow"], input[name^="rentEnd"]').keyup(function(){
                        $(this).val(partMonney($(this).val().replace(".", "")));
                        value = $(this).val().replace(".", "");
                        value = value.replace(".", "");
                        value = value.replace(".", "");
                        value = value.replace(".", "");
                        value = value.replace(".", "");
                        value = value.replace(".", "");
                        value = value.replace(".", "");
                        value = value.replace(".", "");
                        value = value.replace(".", "");
                        $(this).parent().find('div.rcost').html('<div class="blue">'+coverMonneyToString(value)+'</div>');
                    });
                    
                    <?php if($this->holder_list){ ?>
                    var availableHolder = [<?php echo $this->holder_list;?>];
                    $( 'input[name^="holders"]' ).autocomplete({
                        source: availableHolder
                    });
                    <?php } ?>
                    <?php if($this->owner_list){ ?>
                    var availableOwner = [<?php echo $this->owner_list;?>];
                  
                    $( 'input[name^="owner"]' ).autocomplete({
                        source: availableOwner
                    });
                    <?php } ?>
		        });
			});

			function del(obj){
				$(obj).parent().parent().remove();
			}
            function free(itemsId, obj){
                var name=$('.name'.concat(itemsId)).val();
                var e=confirm('Bạn có chuyển '+name+' thành thiết bị tự do không?');
                if(e)
                {
                    $.ajax({
                    type:     "GET",
                    url:      "http://local.cmdb.ssgroup.com.vn/default/item/freedom/id/".concat(itemsId),
                    });
                    $(obj).parent().parent().remove();
                    alert('Đã chuyển thành thiết bị tự do');
                }
                else
                {
                    return false;
                }          
            }
		</script>
    
    	<?php
            if(count($this->data['item'])){
			foreach($this->data['item'] as $item){
			
		?>
				<script language="javascript">
					$("form > dl.zend_form > dd#ipWan-element").append('<div class="fwrap">'
					+'<div class = "mTit">Thông tin thành phần thiết bị</div><div class="content"><input type="hidden" value = "<?php echo $item['itemId']; ?>" name = "itemId[]" />'
					+'<div class="row-2" style="margin-bottom: 10px;"><div class="row-first"><label class="first-row-label">Tên</label> <input type="text" name="item[]" size="20" value = "<?php echo $item['itemName'] ?>"/></div>'
					+'<div class="row-second"><label>Serial number</label> <input type="text" name="serialNumber[]" size="20" value="<?php echo $item['serialNumber']; ?>"/></div><div style="clear:both;"></div></div>'
                    +'<div style="clear:both;"></div><div class = "row-2"><div class="row-first"><label class="first-row-label">Giá Mua</label>  <input type="text" name="priceItem[]" size="20" value = "<?php echo number_format($item['priceItem'], 0, ',', '.') ?>" /><div class="rcost"></div></div>'
					+'<div class="row-second"><label>Giá Thuê</label> <input type="text" name="rentItem[]" size="20" value = "<?php echo number_format($item['rentItem'], 0, ',', '.') ?>" /><div class="rcost"></div></div>'
					+'<div style="clear:both;"></div><div class="row-first"><label class="first-row-label">Giá Hiện Tại</label> <input type="text" name="priceNow[]" size="20"  value = "<?php echo number_format($item['priceNow'], 0, ',', '.') ?>" /><div class="rcost"></div></div>'
					+'<div class="row-second"><label>Giá sau hợp đồng</label> <input type="text" name="rentEnd[]" size="20"   value = "<?php echo number_format($item['rentEnd'], 0, ',', '.') ?>"/><div class="rcost"></div></div>'
                    +'<div class="clr"></div></div><div class = "row-3"><div class="row-first"><label class="first-row-label">Ngày Bắt Đầu</label> <input readOnly ="true" type="text" name="startDate[]" placeholder="mm/dd/yyyy" size="20" class="startDate" value = "<?php echo $this->time()->dataTimeToDateTime_3($item['startDate']) ?>" /></div>'
					+'<div class="row-second"><label>Ngày kết thúc</label> <input type="text" name="duration[]" placeholder="mm/dd/yyyy" size="20" class="duration" value = "<?php echo $this->time()->dataTimeToDateTime_3($item['duration']) ?>" /></div>'
					+'<div class="clr"></div></div><div class = "row-5"><div class="row-first"><label class="first-row-label">Tài Sản</label> <input type="text" name="owner[]" size="20" value = "<?php echo $item['owner'] ?>" /></div>'
					+'<div class="row-second"><label>Người ký hợp đồng</label> <input type="text" name="holders[]" size="20" value = "<?php echo $item['holders'] ?>" /></div>'
					+'<div class="clr"></div></div><div class = "row-4"><label class="first-row-label">Lịch sử</label> <textarea name="history[]" rows="3" cols ="50"><?php echo str_replace("\r\n",' ',$item['history']); ?></textarea></br>'					
					+'<label class="first-row-label">Thông Tin</label> <textarea name="info[]" rows="3" cols ="50"><?php echo str_replace("\r\n",' ',$item['info']); ?></textarea></br>'
					+'<label class="first-row-label">Phụ lục</label> <textarea name="addendums[]" rows="3" cols ="50"><?php echo str_replace("\r\n",' ',$item['addendums']); ?></textarea></br>'
					+'<div style="clear: both;"></div><label class="red first-row-label">Thuê chỗ</label> <input type="checkbox" name="endTime[]" <?php if($item['expire'] == 1) { echo 'checked="true"'; }; ?> />'
					+'</div><input class="del"  type="button" onclick="del(this);" value ="Xóa" />'
                    +' <input type="hidden" class="name<?php echo $item['itemId'];?>" value="<?php echo $item['itemName'];?>"/>'
                    +'<input class="free" style="float:right;margin-top:10px;margin-right:260px;"  type="button" onclick="free(<?php echo $item['itemId']; ?>, this);" value ="Chuyển Thành Thiết Bị Tự Do" /></div></div>');
                    $(function () {
    		            $(".startDate").datepicker({ changeMonth : true, changeYear: true, yearRange: '1950:3000' });
    					$(".duration").datepicker({ changeMonth : true, changeYear: true, yearRange: '1950:3000' });
                        $('input[name^="priceItem"], input[name^="rentItem"], input[name^="priceNow"], input[name^="rentEnd"]').keydown(function(event) {
                        	if( !(event.keyCode == 8                                // backspace
                                || event.keyCode == 9                               // tab
                                || event.keyCode == 46                              // delete
                                || (event.keyCode >= 35 && event.keyCode <= 40)     // arrow keys/home/end
                                || (event.keyCode >= 48 && event.keyCode <= 57)     // numbers on keyboard
                                || (event.keyCode >= 96 && event.keyCode <= 105))   // number on keypad
                                ) {
                                    event.preventDefault();     // Prevent character input
                            }
                        });
                        $('input[name^="priceItem"], input[name^="rentItem"], input[name^="priceNow"], input[name^="rentEnd"]').keyup(function(){
                            $(this).val(partMonney($(this).val().replace(".", "")));
                            value = $(this).val().replace(".", "");
                            value = value.replace(".", "");
                            value = value.replace(".", "");
                            value = value.replace(".", "");
                            value = value.replace(".", "");
                            value = value.replace(".", "");
                            value = value.replace(".", "");
                            value = value.replace(".", "");
                            value = value.replace(".", "");
                            $(this).parent().find('div.rcost').html('<div class="blue">'+coverMonneyToString(value)+'</div>');
                        });
                        
                        <?php if($this->holder_list){ ?>
                        var availableHolder = [<?php echo $this->holder_list;?>];
                        $( 'input[name^="holders"]' ).autocomplete({
                            source: availableHolder
                        });
                        <?php } ?>
                        <?php if($this->owner_list){ ?>
                        var availableOwner = [<?php echo $this->owner_list;?>];
                        $( 'input[name^="owner"]' ).autocomplete({
                            source: availableOwner
                        });
                        <?php } ?>
    		        });
				</script>
		<?php
			}			
        }
		if(isset($this->item)){
				foreach($this->item as $item){ ?>
				<script language="javascript">
					$("form > dl.zend_form > dd#ipWan-element").append('<div class="fwrap"><div class = "mTit">Thông tin thành phần thiết bị</div><div class="content">'
												+'<input type="hidden" value = "<?php echo $item->itemId; ?>" name = "itemId[]" />'
												+'<div class="row-2"><div class="row-first"><label class="first-row-label">Tên</label> <input type="text" name="item[]" size="20" value = "<?php echo $item->itemName; ?>" /></div>'
												+'<div class="row-second"><label>Serial number</label> <input type="text" name="serialNumber[]" size="20" value="<?php echo $item->serialNumber; ?>"/></div><div style="clear:both;"></div></div>'
                                                +'<div style="clear:both;"></div><div class = "row-2"><div class="row-first"><label class="first-row-label">Giá Mua</label>  <input type="text" name="priceItem[]" size="20" value = "<?php echo number_format($item->priceItem, 0, ',', '.'); ?>"/><div class="rcost"></div></div> '
												+'<div class="row-second"><label>Giá Thuê</label> <input type="text" name="rentItem[]" size="20" value = "<?php echo number_format($item->rentItem, 0, ',', '.'); ?>" /><div class="rcost"></div></div>'
												+'<div style="clear:both;"></div><div class="row-first"><label class="first-row-label">Giá Hiện Tại</label> <input type="text" name="priceNow[]" size="20"  value = "<?php echo number_format($item->priceNow, 0, ',', '.'); ?>" /><div class="rcost"></div></div>'
                                                +'<div class="row-second"><label>Giá sau hợp đồng</label> <input type="text" name="rentEnd[]" size="20"  value = "<?php echo number_format($item->rentEnd, 0, ',', '.'); ?>"/><div class="rcost"></div></div>'
												+'<div class="clr"></div></div><div class = "row-3"><div class="row-first"><label class="first-row-label">Ngày Bắt Đầu</label> <input readOnly ="true" type="text" name="startDate[]" placeholder="mm/dd/yyyy" size="20" class="startDate" value = "<?php echo $item->startDate; ?>" /></div>'
												+'<div class="row-second"><label>Ngày kết thúc</label> <input  type="text" name="duration[]" placeholder="mm/dd/yyyy" size="20" class="duration" value = "<?php echo $item->duration; ?>" /></div>'
												+'<div class="clr"></div></div><div class = "row-5"><div class="row-first"><label class="first-row-label">Tài Sản</label> <input type="text" name="owner[]" size="20" value = "<?php echo $item->owner; ?>" /></div>'
												+'<div class="row-second"><label>Người ký hợp đồng</label> <input type="text" name="holders[]" size="20" value = "<?php echo $item->holders; ?>" /></div>'
												+'<div class="clr"></div></div><div class = "row-4"><label class="first-row-label">Lịch sử</label> <textarea name="history[]" rows="3" cols ="50"><?php echo str_replace("\r\n",' ',$item->history); ?></textarea></br>'
												+'<label class="first-row-label">Thông Tin</label> <textarea "name="info[]" rows="3" cols ="50"><?php echo $item->info; ?></textarea></br>'
												+'<label class="first-row-label">Phụ lục</label> <textarea name="addendums[]" rows="3" cols ="50"><?php echo $item->addendums; ?></textarea>'
					                            +'Hết thời hạn thuê : <input type="checkbox" name="endTime[]" />'
												+'</div><input class="del" type="button" onclick="del(this);" value ="Xóa" />'
                                                +' <input type="hidden" class="name<?php echo $item['itemId'];?>" value="<?php echo $item['itemName'];?>"/>'
                                                +'<input class="free" style="float:right;margin-top:10px;margin-right:260px;"  type="button" onclick="free(<?php echo $item['itemId']; ?>, this);" value ="Chuyển Thành Thiết Bị Tự Do" /></div></div>');
                    $(function () {
    		            $(".startDate").datepicker({ changeMonth : true, changeYear: true, yearRange: '1950:3000' });
    					$(".duration").datepicker({ changeMonth : true, changeYear: true, yearRange: '1950:3000' });
                        $('input[name^="priceItem"], input[name^="rentItem"], input[name^="priceNow"], input[name^="rentEnd"]').keydown(function(event) {
                        	if( !(event.keyCode == 8                                // backspace
                                || event.keyCode == 9                               // tab
                                || event.keyCode == 46                              // delete
                                || (event.keyCode >= 35 && event.keyCode <= 40)     // arrow keys/home/end
                                || (event.keyCode >= 48 && event.keyCode <= 57)     // numbers on keyboard
                                || (event.keyCode >= 96 && event.keyCode <= 105))   // number on keypad
                                ) {
                                    event.preventDefault();     // Prevent character input
                            }
                        });
                        $('input[name^="priceItem"], input[name^="rentItem"], input[name^="priceNow"], input[name^="rentEnd"]').keyup(function(){
                            $(this).val(partMonney($(this).val().replace(".", "")));
                            value = $(this).val().replace(".", "");
                            value = value.replace(".", "");
                            value = value.replace(".", "");
                            value = value.replace(".", "");
                            value = value.replace(".", "");
                            value = value.replace(".", "");
                            value = value.replace(".", "");
                            value = value.replace(".", "");
                            value = value.replace(".", "");
                            $(this).parent().find('div.rcost').html('<div class="blue">'+coverMonneyToString(value)+'</div>');
                        });
                            
                        <?php if($this->holder_list){ ?>
                        var availableHolder = [<?php echo $this->holder_list;?>];
                        $( 'input[name^="holders"]' ).autocomplete({
                            source: availableHolder
                        });
                        <?php } ?>
                        <?php if($this->owner_list){ ?>
                        var availableOwner = [<?php echo $this->owner_list;?>];
                        $( 'input[name^="owner"]' ).autocomplete({
                            source: availableOwner
                        });
                        <?php } ?>
    		        });
				</script>
			<?php if(!isset($item->itemId)){ ?>
					<script language="javascript">
						$("form > dl.zend_form > dd#ipWan-element").append('<div class="fwrap"><div class = "mTit">Thông tin thành phần thiết bị</div><div class="content">'
						+'<div class="row-2"><div class="row-first"><label class="first-row-label">Tên</label> <input type="text" name="item_new[]" size="30" value = "<?php echo $item->itemName; ?>" /></div>'
						+'<div class="row-second"><label>Serial number</label> <input type="text" name="serialNumber[]" size="20" value="<?php echo $item->serialNumber; ?>"/></div><div style="clear:both;"></div></div>'
                        +'<div style="clear:both;"></div><div class = "row-2"><div class="row-first"><label class="first-row-label">Giá Mua</label>  <input type="text" name="priceItem_new[]" size="20" value = "<?php echo $item->priceItem; ?>" /><div class="rcost"></div></div>'
						+'<div class="row-second"><label>Giá Thuê</label> <input type="text" name="rentItem_new[]" size="20" value = "<?php echo $item->rentItem; ?>" /><div class="rcost"></div></div>'
						+'<div style="clear:both;"></div><div class="row-first"><label class="first-row-label">Giá Hiện Tại</label> <input type="text" name="priceNow[]" size="20"  value = "<?php echo $item->priceNow; ?>" /><div class="rcost"></div></div>'
                        +'<div class="row-second"><label>Giá sau hợp đồng</label> <input type="text" name="rentEnd[]" size="20"   value = "<?php echo $item->rentEnd; ?>"/><div class="rcost"></div></div>'
						+'<div class="clr"></div></div><div class = "row-3"><div class="row-first"><label class="first-row-label">Ngày Bắt Đầu</label> <input readOnly ="true" type="text" name="startDate[]" placeholder="mm/dd/yyyy" size="20" class="startDate" value = "<?php echo $item->startDate; ?>" /></div>'
						+'<div class="row-second"><label>Ngày kết thúc</label> <input  type="text" name="duration[]" placeholder="mm/dd/yyyy" size="20" class="duration" value = "<?php echo $item->duration; ?>" /></div>'						
						+'<div class="clr"></div></div><div class = "row-5"><div class="row-first"><label class="first-row-label">Tài Sản</label> <input type="text" name="owner[]" size="20" value = "<?php echo $item->owner; ?>" /></div>'
						+'<div class="row-second"><label>Người ký hợp đồng</label> <input type="text" name="holders[]" size="20" value = "<?php echo $item->holders; ?>" /></div>'
						+'<div class="clr"></div></div><div class = "row-4"><label class="first-row-label">Lịch sử</label> <textarea name="history[]" rows="3" cols ="50"><?php echo str_replace("\r\n",' ',$item->history); ?></textarea></br>'						
						+'<label class="first-row-label">Thông Tin</label> <textarea "name="info[]" rows="3" cols ="50"><?php echo $item->info; ?></textarea></br>'
						+'<label class="first-row-label">Phụ lục</label> <textarea name="addendums[]" rows="3" cols ="50"><?php echo $item->addendums; ?></textarea>'
					    +'Hết thời hạn thuê : <input type="checkbox" name="endTime[]" />'
						+'</div><input class="del" type="button" onclick="del(this);" value ="Xóa" /></div></div>');
					</script>

			<?php }
				}
			}
			//edit item :: 07/11/2012 ::

        ?>
    </div>
</div>
